package capricornus.core
{
	import capricornus.actor.ActorManager;
	import capricornus.events.RefreshEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.getTimer;

	[Event(name="refresh", type="capricornus.events.RefreshEvent")]
	public class RefreshManager extends EventDispatcher
	{
		private static var _ins:RefreshManager = null;
		
		private var _isListener:Boolean = false;
		
		public function RefreshManager()
		{
			init();
		}
		
		public static function get instance():RefreshManager
		{
			if( null==_ins )
			{
				_ins = new RefreshManager();
			}
			
			return _ins;
		}
		
		public function get running():Boolean
		{
			return _isListener;
		}
		
		public function start():void
		{
			if( !_isListener )
			{
				_isListener = true;
				_lastTime = getTimer();
				StageManager.stage.addEventListener( Event.ENTER_FRAME, onEnterFrame, false, 0, true );
			}
		}
		
		public function stop():void
		{
			if( _isListener )
			{
				_isListener = false;
				StageManager.stage.removeEventListener( Event.ENTER_FRAME, onEnterFrame );
			}
		}
		
		private function init():void
		{
		}
		
		private var _lastTime:Number = 0;
		private function onEnterFrame( e:Event ):void
		{
			var t:Number = getTimer();
			
			ActorManager.instance.refreshAllActor( 20/*t - _lastTime*/ );
			
			dispatchEvent( new RefreshEvent( RefreshEvent.REFRESH, t - _lastTime, 20 ) );
			_lastTime = t;
		}
	}
}